/**
 * 生成视频合并命令
 */

const path = require("path");
const fs = require("fs");
const ffmpegPath = path.join(__dirname , '../', 'ffmpeg', 'ffmpeg.exe');

function merge_video(filepath, savePath, newname, breakImage, breakMp3, imageSize, rate) {

    imageSize = imageSize ? `-y -s ${imageSize}` : '';
    let imagePath;
    let mp3Path;
    let cmds = [];

    if (breakImage) {
        imagePath = path.join(savePath, 'images');

        if (!fs.existsSync(imagePath)) {
            fs.mkdirSync(imagePath, { recursive: true });
            let cmdStr = `
                "${ffmpegPath}" -i "${filepath}" -r ${rate} -f image2 ${imageSize} "${path.join(imagePath, `%d-${newname}.jpg`)}"
            `;
            cmds.push(cmdStr.trim());
        }
    }

    if (breakMp3) {
        mp3Path = path.join(savePath, 'mp3');
        
        if (!fs.existsSync(mp3Path)) {
            fs.mkdirSync(mp3Path, { recursive: true });
            let cmdStr = `
                "${ffmpegPath}" -i "${filepath}" -b:a 192K -vn "${path.join(mp3Path, newname)}.mp3"
            `;
            cmds.push(cmdStr.trim());
        }
    }





    

    return cmds;
}

module.exports = merge_video;